package com.darren.befriend;

import java.security.MessageDigest;
import java.util.List;

import com.darren.befriend.utilities.LogUtil;
import com.darren.befriend.utilities.MySharedPreferences;
import com.parse.LogInCallback;
import com.parse.ParseException;
import com.parse.ParseUser;

import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.Signature;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import android.view.Window;

public class SplashScreenActivity extends BaseActivity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.darren.befriend.BaseActivity#addContent()
	 */
	@Override
	protected int addContent() {
		// TODO Auto-generated method stub
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		return R.layout.activity_splash_screen;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.darren.befriend.BaseActivity#initView()
	 */
	@Override
	protected void initView() {
		// TODO Auto-generated method stub
		super.initView();

		// ParseUser currentUser = ParseUser.getCurrentUser();
		// if (currentUser != null && isPush) {
		//
		// Intent intent = new Intent(SplashScreenActivity.this,
		// HomeActivity.class);
		// intent.putExtra(Constant.IS_START_FROM_PUSH, isPush);
		// startActivity(intent);
		// finish();
		// } else {
		PackageInfo info;
		try {
			info = getPackageManager().getPackageInfo(getPackageName(),
					PackageManager.GET_SIGNATURES);
			for (Signature signature : info.signatures) {
				MessageDigest md;
				md = MessageDigest.getInstance("SHA");
				md.update(signature.toByteArray());
				String something = new String(Base64.encode(md.digest(), 0));
				Log.e("hash key", something);
			}

		} catch (Exception e) {
			Log.e("exception", e.toString());
		}

		new PrefetchDataAsynTask().execute("");
		// }
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onBackPressed()
	 */
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		// super.onBackPressed();

		return;
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	/**
	 * Get current user locale, news and indexes data
	 * */
	private class PrefetchDataAsynTask extends
			AsyncTask<String, String, String> {

		@Override
		protected String doInBackground(String... params) {
			// Sleep thread to display splash screen
			try {

				String username = MySharedPreferences.getPreferences(
						MySharedPreferences.PREF_USERNAME, "");
				if (!username.equals("")) {

					String password = MySharedPreferences.getPreferences(
							MySharedPreferences.PREF_PASSWORD, "");
					if (password.equals("")) {

						password = "kki";
					}

					LogUtil.e("SplashScreenActivity initView", "username: "
							+ username);

					// Get user information from parse
					ParseUser.logInInBackground(username, password,
							new LogInCallback() {

								@Override
								public void done(final ParseUser user,
										ParseException arg1) {
									// TODO Auto-generated method stub
									if (user != null) {

										user.put("Status", true);
										try {

											user.save();
										} catch (ParseException e) {
											// TODO Auto-generated catch block
											LogUtil.e("user.save()",
													e.getMessage());
										}

										LogUtil.e("logInInBackground",
												user.getUsername());

										List<String> enemies = user
												.getList("Enemy");
										if (enemies != null) {
											for (int i = 0; i < enemies.size(); i++) {

												LogUtil.e(
														"logInInBackground - enemy : ",
														enemies.get(i));
											}

											user.put("Enemy", enemies);
										}
										try {

											user.put("MeInfo", user
													.getParseObject("MeInfo")
													.fetch());
										} catch (ParseException e) {

											// TODO Auto-generated catch block
											LogUtil.e("MeInfo", e.getMessage());
										}

										try {

											user.put("Filter", user
													.getParseObject("Filter")
													.fetch());
										} catch (ParseException e) {

											// TODO Auto-generated catch block
											LogUtil.e("Filter", e.getMessage());
										}

										if (mGeoPoint != null) {

											user.put("Position", mGeoPoint);
										}

										mParseUser = user;
										// Start home activity
										Intent intent = new Intent(
												SplashScreenActivity.this,
												HomeActivity.class);
										// intent.putExtra(
										// Constant.IS_START_FROM_PUSH,
										// isPush);
										startActivity(intent);
										finish();
									} else {

										LogUtil.e("logInInBackground",
												arg1.getMessage());
										MySharedPreferences
												.setPreferences(
														MySharedPreferences.PREF_PASSWORD,
														"");
										MySharedPreferences
												.setPreferences(
														MySharedPreferences.PREF_USERNAME,
														"");
										Intent intent = new Intent(
												SplashScreenActivity.this,
												LoginActivity.class);
										startActivity(intent);
										finish();
									}
								}
							});

				} else {

					LogUtil.e("logInInBackground", "username blank");
					Intent intent = new Intent(SplashScreenActivity.this,
							LoginActivity.class);
					startActivity(intent);
					finish();
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			return null;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
		 */
		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
		}
	}

}
